package main.scripts;

import java.awt.Graphics;

import main.bots.Bot;
import main.bots.Methods;
import main.bots.Script;
import main.hooks.RSInterface;
import main.hooks.RSInterfaceChild;
import main.hooks.RSNPC;
import main.hooks.RSObject;
import main.hooks.RSTile;
import main.listeners.PaintListener;
import main.listeners.ServerMessageListener;

public class PestControl extends Script implements Bot, ServerMessageListener {
	
	public static final RSObject Gangplank = new RSObject(2658,2639, 14315);
	public static RSTile boat = new RSTile(2657, 2611);
	public static int status = 0;
	public static boolean toEnd = false;
	public static String enterBoat = "You enter the boat";
	private boolean enteredBoat;
	public int gamesPlayed = 0;
	public RSTile[] path = { new RSTile(2658, 2604), new RSTile(2656, 2591), new RSTile(2656, 2585) };

	@Override
	public boolean endCondition() {
		// TODO Auto-generated method stub
		return toEnd;
	}
	public void attackPortal() {
		
	}
	@Override
	public String[] getAuthors() {
		// TODO Auto-generated method stub
		return null;
	}
	public boolean inEntranceBoat() {
		return inSquare(2663, 2643, 2660, 2638);
	}
public boolean inGameBoat() {
	return inSquare(2659, 2614, 2656, 2608);
	}
	@Override
	public String getCategory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public double getVersion() {
		// TODO Auto-generated method stub
		return 1;
	}
	public boolean needsMove() {
		return getMyLocation().getX() == 2660 && getMyLocation().getY() == 2639;
	}
	public int getScore() {
		String s = getRSInterface(15897).getText();
		if( s == null || !s.contains("Points") || s.length() < 7 || s.equals("")) {
			return 0;
		}
		return Integer.parseInt(getRSInterface(15897).getText().trim().substring(8));
	}
	@Override
	public int loop() {
		if(isInCombat()) {
			debug("Attacking portal maybe");
			return 1000;
		}
		
		if(needsMove()) {
			debug("walking randomly");
			wait(random(750, 1500));
			RSTile test = randomTileInSquare(2663, 2643, 2660, 2638);
			walkTo(test);
			wait(1000);
			return random(100, 2000);
		}
		if(getMyLocation().getX() == 2657 && getMyLocation().getY() == 2639) {
			
			if(enteredBoat)
				resetGame();
			Methods.getClientToUse().stream.clickObject132(Gangplank.getId(), Gangplank.getX(), Gangplank.getY());
			debug("Entering boat");
			return 500;
		}
		
	
		if(enteredBoat) { //Ingame
			if(getMyLocation().getX() == 2656 && getMyLocation().getY() == 2585) {
				openDoor();
				return 1000;
			}
		RSNPC npc = getClosestNpcToAttack("Portal");
		if(npc == null) {
			debug("Walking there");
			walkPath(path);
			
			return 1000;
		}
		debug("Npc found: " + npc.getName() + " x: " + npc.getX() + " y: " + npc.getY() + " ? " + npc.getNpc().entScreenX);
		if(distanceTo(npc.getLocation()) > 8) {
			walkTo(npc.getLocation());
		}
		//walkPath(path);
			attackNpc(npc);
			//debug("Walking");
		
			return 1000;
		}
			
		if(inEntranceBoat()) {
			debug("waiting for new game");
			try {
			debug("Points: " + getScore());
			} catch(Exception e) {
				e.printStackTrace();
			}
			return 1000;
		}
		if(inGameBoat()) {
			enteredBoat = true;
			debug("camping rofl");
			return 1000;
		}
		return 1000;
	}
	private void openDoor() {
		atObject132(14233, 2656, 2585);
	}
	public void resetGame() {
		System.out.println("---------------------------------------");
		enteredBoat = false;
		status = 0;
		gamesPlayed++;
		wait(3000);
		sendCommand("save");
		
		System.out.println("You have played: " + gamesPlayed + " games");
		System.out.println("---------------------------------------");
	}
	@Override
	public void onFinish() {
		System.out.println("bye :(");
		
	}

	@Override
	public boolean onStart() {
		System.out.println("Starting..");
		return true;
	}

	@Override
	public void serverMessageRecieved(String message) {
		System.out.println("New Message Recieved: " + message);
		if(message.contains("You enter the boat")) {
		
		
		}
		
	}

	@Override
	public String getDescription() {
		return "Pest control for most delta servers";
	}

	@Override
	public String getScriptName() {
	return "Pest Control";
	}

	

}
